/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if(null == root) {
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        TreeNode cur = root;
        int size = 0;
        while(!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            size = queue.size();
            for(int i = 0; i < size;i++) {
                cur = queue.poll();
                tmp.add(cur.val);
                if(null != cur.left) {
                    queue.offer(cur.left);
                }
                if(null!= cur.right) {
                    queue.offer(cur.right);
                }
            }
            list.add(tmp);
        }
        return list;
    }
}